package com.facebook.drawee.generic;

import android.content.res.Resources;
import android.graphics.ColorFilter;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;

import com.facebook.common.internal.Preconditions;
import com.facebook.drawee.drawable.ArrayDrawable;
import com.facebook.drawee.drawable.DrawableParent;
import com.facebook.drawee.drawable.FadeDrawable;
import com.facebook.drawee.drawable.ForwardingDrawable;
import com.facebook.drawee.drawable.MatrixDrawable;
import com.facebook.drawee.drawable.ScaleTypeDrawable;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.interfaces.SettableDraweeHierarchy;

import java.util.Arrays;
import java.util.Iterator;

import javax.annotation.Nullable;

public final class GenericDraweeHierarchy implements SettableDraweeHierarchy {
    public RoundingParams mRoundingParams;
    public final FadeDrawable mFadeDrawable;
    public final int ACTUAL_IMAGE_INDEX;
    private final Drawable mEmptyActualImageDrawable;
    private final Resources mResources;
    private final RootDrawable mTopLevelDrawable;
    private final ForwardingDrawable mActualImageWrapper;
    private final int PLACEHOLDER_IMAGE_INDEX;
    private final int PROGRESS_BAR_IMAGE_INDEX;
    private final int RETRY_IMAGE_INDEX;
    private final int FAILURE_IMAGE_INDEX;

    GenericDraweeHierarchy(GenericDraweeHierarchyBuilder builder) {
        int v1 = 0;
        this.mEmptyActualImageDrawable = new ColorDrawable(0);
        this.mResources = builder.mResources;
        this.mRoundingParams = builder.mRoundingParams;
        this.mActualImageWrapper = new ForwardingDrawable(this.mEmptyActualImageDrawable);
        int backgroundImageSize = builder.mBackgroundImageList != null ? builder.mBackgroundImageList.size() : 0;
        int overlaySize = builder.mOverlays != null ? builder.mOverlays.size() : 0;
        int pressedStateOverlaySize = builder.mPressedStateOverlay != null ? 1 : 0;
        int numOverlays = overlaySize + pressedStateOverlaySize;
        overlaySize = backgroundImageSize + 1;
        this.PLACEHOLDER_IMAGE_INDEX = backgroundImageSize;
        pressedStateOverlaySize = overlaySize + 1;
        this.ACTUAL_IMAGE_INDEX = overlaySize;
        overlaySize = pressedStateOverlaySize + 1;
        this.PROGRESS_BAR_IMAGE_INDEX = pressedStateOverlaySize;
        pressedStateOverlaySize = overlaySize + 1;
        this.RETRY_IMAGE_INDEX = overlaySize;
        int v5 = pressedStateOverlaySize + 1;
        this.FAILURE_IMAGE_INDEX = pressedStateOverlaySize;
        Drawable[] layers = new Drawable[v5 + numOverlays];
        if (backgroundImageSize > 0) {
            Iterator<Drawable> iterator = builder.mBackgroundImageList.iterator();
            for (overlaySize = 0; iterator.hasNext(); ++overlaySize) {
                layers[overlaySize] = this.buildBranch((Drawable) iterator.next(), null);
            }
        }
        layers[this.PLACEHOLDER_IMAGE_INDEX] = this.buildBranch(builder.mPlaceHolderImage, builder.mPlaceHolderImageScaleType);
        ScalingUtils.ScaleType scaleType = builder.mActualImageScaleType;
        mActualImageWrapper.setColorFilter((ColorFilter) null);
        layers[ACTUAL_IMAGE_INDEX] = WrappingUtils.maybeWrapWithScaleType(WrappingUtils.b(mActualImageWrapper, scaleType));
        layers[PROGRESS_BAR_IMAGE_INDEX] = this.buildBranch(builder.mProgressBarImage, builder.mProgressBarImageScaleType);
        layers[RETRY_IMAGE_INDEX] = this.buildBranch(builder.mRetryImage, builder.mRetryImageScaleType);
        layers[FAILURE_IMAGE_INDEX] = this.buildBranch(builder.mFailureImage, builder.mFailureImageScaleType);
        if (numOverlays > 0) {
            if (builder.mOverlays != null) {
                Iterator<Drawable> iterator = builder.mOverlays.iterator();
                while (iterator.hasNext()) {
                    layers[v1 + v5] = this.buildBranch((Drawable) iterator.next(), null);
                    ++v1;
                }
            }
            if (builder.mPressedStateOverlay != null) {
                layers[v5 + v1] = this.buildBranch(builder.mPressedStateOverlay, null);
            }
        }
        this.mFadeDrawable = new FadeDrawable(layers);
        this.mFadeDrawable.setTransitionDuration(builder.DEFAULT_FADE_DURATION);
        this.mTopLevelDrawable = new RootDrawable(WrappingUtils.maybeWrapWithRoundedOverlayColor(this.mFadeDrawable, this.mRoundingParams));
        this.mTopLevelDrawable.mutate();
        this.resetFade();
    }

    @Nullable
    private Drawable buildBranch(@Nullable Drawable arg3, @Nullable ScalingUtils.ScaleType arg4) {
        return WrappingUtils.maybeWrapWithScaleType(WrappingUtils.maybeApplyLeafRounding(arg3, this.mRoundingParams, this.mResources), arg4);
    }

    private void setProgress(float progress) {
        Drawable drawable = this.getParentDrawableAtIndex(this.PROGRESS_BAR_IMAGE_INDEX).getDrawable();
        if (drawable != null) {
            if (progress >= 0.999f) {
                if ((drawable instanceof Animatable)) {
                    ((Animatable) drawable).stop();
                }
                this.fadeOutLayer(this.PROGRESS_BAR_IMAGE_INDEX);
            } else {
                if ((drawable instanceof Animatable)) {
                    ((Animatable) drawable).start();
                }
                this.fadeInLayer(this.PROGRESS_BAR_IMAGE_INDEX);
            }
            drawable.setLevel(Math.round(10000f * progress));
        }
    }

    public final DrawableParent getParentDrawableAtIndex(final int index) {
        Preconditions.checkArgument(index >= 0);
        Preconditions.checkArgument(index < mFadeDrawable.mDrawableParents.length);
        if (mFadeDrawable.mDrawableParents[index] == null) {
            mFadeDrawable.mDrawableParents[index] = new DrawableParent() {

                public final Drawable getDrawable() {
                    return mFadeDrawable.getDrawable(index);
                }

                public final Drawable setCurrent(Drawable drawable) {
                    return mFadeDrawable.setDrawable(index, drawable);
                }
            };
        }
        DrawableParent drawableParent = mFadeDrawable.mDrawableParents[index];
        if ((drawableParent.getDrawable() instanceof MatrixDrawable)) {
            drawableParent = (DrawableParent) drawableParent.getDrawable();
        }

        if ((drawableParent.getDrawable() instanceof ScaleTypeDrawable)) {
            return (DrawableParent) drawableParent.getDrawable();
        }

        return drawableParent;
    }

    public final Drawable getTopLevelDrawable() {
        return this.mTopLevelDrawable;
    }

    public final void setActualImageColorFilter(ColorFilter colorFilter) {
        this.mActualImageWrapper.setColorFilter(colorFilter);
    }

    public final void setImage(Drawable drawable, float progress, boolean immediate) {
        drawable = WrappingUtils.maybeApplyLeafRounding(drawable, this.mRoundingParams, this.mResources);
        drawable.mutate();
        this.mActualImageWrapper.setCurrentWithoutInvalidate(drawable);
        this.mFadeDrawable.beginBatchMode();
        this.fadeOutBranches();
        this.fadeInLayer(this.ACTUAL_IMAGE_INDEX);
        this.setProgress(progress);
        if (immediate) {
            this.mFadeDrawable.finishTransitionImmediately();
        }
        this.mFadeDrawable.endBatchMode();
    }

    public final void setRoundingParams(RoundingParams roundingParams) {
        this.mRoundingParams = roundingParams;
        WrappingUtils.updateOverlayColorRounding((DrawableParent) this.mTopLevelDrawable, this.mRoundingParams);
        for (int i = 0; i < this.mFadeDrawable.mLayers.length; ++i) {
            WrappingUtils.updateLeafRounding(this.getParentDrawableAtIndex(i), this.mRoundingParams, this.mResources);
        }
    }

    public final void reset() {
        this.mActualImageWrapper.setCurrentWithoutInvalidate(this.mEmptyActualImageDrawable);
        this.resetFade();
    }

    public final void setChildDrawableAtIndex(int arg5) {
        Drawable drawable = this.mResources.getDrawable(arg5);
        if (drawable == null) {
            this.mFadeDrawable.setDrawable(this.PLACEHOLDER_IMAGE_INDEX, null);
        } else {
            this.getParentDrawableAtIndex(this.PLACEHOLDER_IMAGE_INDEX)
                    .setCurrent(WrappingUtils.maybeApplyLeafRounding(drawable, this.mRoundingParams, this.mResources));
        }
    }

    private void fadeInLayer(int index) {
        if (index >= 0) {
            mFadeDrawable.mTransitionState = 0;
            mFadeDrawable.mIsLayerOn[index] = true;
            mFadeDrawable.invalidateSelf();
        }
    }

    public final void setProgress() {
        this.mFadeDrawable.beginBatchMode();
        this.setProgress(0f);
        this.mFadeDrawable.finishTransitionImmediately();
        this.mFadeDrawable.endBatchMode();
    }

    private void fadeOutLayer(int index) {
        if (index >= 0) {
            mFadeDrawable.mTransitionState = 0;
            mFadeDrawable.mIsLayerOn[index] = false;
            mFadeDrawable.invalidateSelf();
        }
    }

    public final void setFailure() {
        this.mFadeDrawable.beginBatchMode();
        this.fadeOutBranches();
        if (this.mFadeDrawable.getDrawable(this.FAILURE_IMAGE_INDEX) != null) {
            this.fadeInLayer(this.FAILURE_IMAGE_INDEX);
        } else {
            this.fadeInLayer(this.PLACEHOLDER_IMAGE_INDEX);
        }
        this.mFadeDrawable.endBatchMode();
    }

    public final void setRetry() {
        this.mFadeDrawable.beginBatchMode();
        this.fadeOutBranches();
        if (this.mFadeDrawable.getDrawable(this.RETRY_IMAGE_INDEX) != null) {
            this.fadeInLayer(this.RETRY_IMAGE_INDEX);
        } else {
            this.fadeInLayer(this.PLACEHOLDER_IMAGE_INDEX);
        }
        this.mFadeDrawable.endBatchMode();
    }

    public final void setControllerOverlay() {
        mTopLevelDrawable.mControllerOverlay = null;
        mTopLevelDrawable.invalidateSelf();
    }

    private void resetFade() {
        this.mFadeDrawable.beginBatchMode();
        mFadeDrawable.mTransitionState = 0;
        Arrays.fill(mFadeDrawable.mIsLayerOn, true);
        mFadeDrawable.invalidateSelf();
        this.fadeOutBranches();
        this.fadeInLayer(this.PLACEHOLDER_IMAGE_INDEX);
        this.mFadeDrawable.finishTransitionImmediately();
        this.mFadeDrawable.endBatchMode();
    }

    private void fadeOutBranches() {
        this.fadeOutLayer(this.PLACEHOLDER_IMAGE_INDEX);
        this.fadeOutLayer(this.ACTUAL_IMAGE_INDEX);
        this.fadeOutLayer(this.PROGRESS_BAR_IMAGE_INDEX);
        this.fadeOutLayer(this.RETRY_IMAGE_INDEX);
        this.fadeOutLayer(this.FAILURE_IMAGE_INDEX);
    }
}

